package utiliy

import com.typesafe.config.Config
import models.baseinfo.{CustomerCode, GYItemCode, GYItemCodeRepository, KDCustomerCodeRepository, KDStockCode, KDStockCodeRepository}
import play.api.cache.SyncCacheApi
import play.api.{ConfigLoader, Configuration}

import javax.inject.Inject
import scala.concurrent.Await
import scala.concurrent.duration._

class OutFilterConfig @Inject()(
                                 config: Configuration,
                                 cache: SyncCacheApi,
                                 itemCodeRepository: GYItemCodeRepository,
                                 customerCodeRepository: KDCustomerCodeRepository,
                                 stockCodeRepository: KDStockCodeRepository
                               ) {
  /**
   */
  val conf = config.get[OutFilter]("")
  private val outFShopNumber: Array[String] = conf.outFShopNumber
  private val outFStockNumber: Array[String] = conf.outFStockNumber
  private val outFstockNumberName = Array(
    ("WR004", "礼顿三水小件退货仓"),
    ("WA101", "礼顿三水小件仓"),
    ("W02115", "佛山三水仓(唯品会)"),
    ("W02132", "佛山三水小件内购仓"),
    ("W02114S", "武汉高桥仓(唯品会省仓)"),
    ("W02130", "佛山三水小件退货仓"),
    ("W02131", "佛山三水小件仓"),
    ("W02129", "江西植护小件仓"),
    ("W02126", "佛山狮山小件内购仓"),
    ("W02114", "武汉高桥仓(唯品会)"),
    ("W02125", "佛山合润小件仓"),
    ("W02124", "佛山狮山小件退货仓"),
    ("W02123", "佛山狮山小件仓"),
    ("W02122", "河北洁雅小件仓"),
    ("WY01", "武汉高桥小件仓"),
    ("355556526", "佛山京东小件仓"),
    ("595-003C", "晋江中通云仓"),
    ("W02111", "广州韵达小件仓"),
    ("800000051", "武汉聚信内购仓"),
    ("W02110", "武汉聚信退货仓"),
    ("800000105", "武汉聚信云仓"),
    ("W02108", "XC华宝小件仓(五羊旗舰店)"),
    ("W02107", "XC武汉高桥小件仓(五羊旗舰店)"),
    ("W02106", "XC金宏昌小件仓(五羊旗舰店)"),
    ("W02105", "XC东发靖江小件仓(五羊旗舰店)"),
    ("W02104", "XC东发句容小件仓(五羊旗舰店)"),
    ("W02103", "XC美满小件仓(五羊旗舰店)"),
    ("W02102", "XC一个苹果小件仓(五羊旗舰店)"),
    ("W02101", "XC优贝思小件仓(五羊旗舰店)"),
    ("W02021", "江西美满退货仓(小件)"),
    ("W02020", "民科园退货仓(小件)"),
    ("W02019", "滕州华宝小件仓"),
    ("W02018", "武汉高桥小件仓"),
    ("W02017", "长沙创一小件仓"),
    ("W02016", "武汉永怡小件仓"),
    ("W02015", "民科园小件仓"),
    ("W02014", "福建金宏昌小件仓"),
    ("W02013", "东发靖江小件仓"),
    ("W02012", "东发句容小件仓"),
    ("W02011", "江西美满小件仓"),
    ("W02009", "杭州嘉杰小件仓"),
    ("W02008", "广州沙亭小件仓"),
    ("W02007", "江西一个苹果仓"),
    ("W02005", "福建可爱多小件仓"),
    ("SCM04", "武汉宝灿B仓"),
    ("SCM03", "武汉宝灿A仓"),
    ("SCM02", "湖州优贝思仓"),
    ("11", "K3测试仓"),
    ("02", "广州沙亭退货仓"),
  )

  lazy val shopInfo = cache.getOrElseUpdate[Seq[CustomerCode]]("OutFilterConfig.shopInfo", 1.hours) {
    Await.result(customerCodeRepository.list, Duration.Inf)
    //.map(c => CustomerInfo(gyId = c.code, kdId = c.kdNo, FSaleOrgId = c.org, FDeliveryDeptID = c.deptId))
  }

  lazy val stockInfo = cache.getOrElseUpdate[Seq[KDStockCode]]("OutFilterConfig.stockInfo", 1.hours) {
    Await.result(stockCodeRepository.list, Duration.Inf)
  }

  lazy val itemInfo = cache.getOrElseUpdate[Seq[GYItemCode]]("OutFilterConfig.itemInfo", 1.hours) {
    Await.result(itemCodeRepository.list, Duration.Inf)
  }

  def isInFStockNumber(num: String) = if (this.outFStockNumber.filter(f => f == num).nonEmpty) true else false

  def isInFShopNumber(num: String) = if (this.outFShopNumber.filter(f => f == num).nonEmpty) true else false

  def getOutStockName(num: String) = {
    val s = this.stockInfo.filter(_.kdFTHIRDSTOCKNO == num)
    if (s.nonEmpty) Some(s.head.kdFName) else None
  }

  //  val shopNumber = Array(
  //    CustomerInfo(gyId = "C010009-1", kdId = "C010009", FSaleOrgId = "101", FStockOrgId = "103", FDeliveryDeptID = "B0802")  // FDeliveryDeptID = "B0802"
  //  )

  /**
   * 小件代发2B订单店铺代码
   * 如 唯品会JITX店铺
   *
   * @param num 管易店铺代码
   */
  def isToBShop(num: String): Boolean = {
    //    if (shopNumber.filter(f => f.gyId == num).nonEmpty) true else false
    val c = shopInfo.filter(f => f.code == num)
    if (c.nonEmpty && c.head.deptId.nonEmpty) true else false
  }

  def getShopInfo(num: String): CustomerCode = {
    //    val tmp = shopNumber.filter(f => f.gyId == num)
    val tmp = shopInfo.filter(f => f.code == num)
    if (tmp.isEmpty) CustomerCode() else tmp.head
  }

  /**
   * 管易仓库代码对应金蝶仓库代码
   *
   * @param num 管易仓库代码
   * @return 金蝶仓库代码
   */
  def getStockInfo(num: String) = {
    val tmp = stockInfo.filter(f => f.kdFTHIRDSTOCKNO == num)
    if (tmp.isEmpty) KDStockCode(kdFNumber = num, kdFTHIRDSTOCKNO = num, org = "103") else tmp.head
  }

  def getAllStockArr() = {
    //   this.outFstockNumberName.toSeq
    Seq(
      ("W01001", "华南成品仓"),
      ("W01005", "华南退货仓"),
      ("W01007", "华南待返工仓"),
      ("W01008", "民科园成品仓"),
      ("W01014", "太和大沥退货仓"),
      ("W01021", "广州沙亭成品仓"),
      ("W01022", "广州沙亭退货仓"),
      ("W02001", "华南小件仓"),
    )

    Seq(
      ("W01103", "佛山三水大件仓"),
      ("W01105", "佛山三水大件备货仓"),
      ("W02132", "佛山三水小件内购仓"),
      ("W01008", "民科园成品仓"),
      ("W01108", "三水美鑫大件仓"),
    )
  }
}

//case class CustomerInfo(gyId: String = "", kdId: String = "", FSaleOrgId: String = "", FStockOrgId: String = "", FDeliveryDeptID: String = "")

case class OutFilter(outFStockNumber: Array[String], outFShopNumber: Array[String])

object OutFilter {
  implicit val configLoader: ConfigLoader[OutFilter] = new ConfigLoader[OutFilter] {
    override def load(config: Config, path: String): OutFilter = {
      val conf = config.getConfig("outfilter.config")
      import scala.jdk.javaapi.CollectionConverters.asScala
      OutFilter(
        outFStockNumber = asScala(conf.getStringList("outFStockNumber")).toArray,
        outFShopNumber = asScala(conf.getStringList("outFShopNumber")).toArray
      )
    }
  }
}

case class ServerSetting(product:Boolean)
object ServerSetting {
  implicit val configLoader: ConfigLoader[ServerSetting] = new ConfigLoader[ServerSetting] {
    override def load(config: Config, path: String): ServerSetting = {
      val conf = config.getConfig("server.setting")
      ServerSetting(
        product = conf.getBoolean("product")
      )
    }
  }
}
